/**   
 * Copyright © 2019 eSunny Info. Tech Ltd. All rights reserved.
 * 
 * @Description:TODO(功能)
 * @className OrderDetailsServiceImpl.java
 * @Package: com.erp.service 
 * @author: yj   
 * @date: 2019年2月22日 下午2:00:42 
 */
package com.erp.service;

import java.sql.Array;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.erp.dao.OrderDaoImpl;
import com.erp.dao.OrderDetailsDaoImpl;
import com.erp.po.Customers;
import com.erp.po.Employees;
import com.erp.po.OrderDetail;
import com.erp.po.Orders;
import com.erp.po.Products;
import com.erp.util.DBHelper;

/**
 * @className OrderDetailsServiceImpl.java
 * @author yj
 * @Description:TODO(功能)
 * @Date 2019年2月22日
 */
public class OrderDetailsServiceImpl implements OrderDetailsService {

	private OrderDetailsDaoImpl odd;
	private OrderDaoImpl odi;
	
	public OrderDetailsServiceImpl() {
		odd = new OrderDetailsDaoImpl();
		odi = new OrderDaoImpl();
	}
	
	/* 
	 * 批量增加订单详情
	 */
	@Override
	public boolean addOrderDetails(Orders order, OrderDetail... orderDetail) {

		Connection conn = null;
		
		try {
			conn = DBHelper.getConnection();
			// 事务处理
			conn.setAutoCommit(false);
			
			// 新建订单
			odi.insertOrder(conn, order);
			// 在添加 订单详情之前 必须先添加 订单
			odd.insertMoveOrderDetails(conn, orderDetail);
			
			conn.commit();
			conn.setAutoCommit(true);
			
			return true;
			
		} catch (SQLException e) {
			
			try {
				conn.rollback();
				conn.setAutoCommit(true);
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
			
			e.printStackTrace();
		} finally {
			odi.close();
			odd.close();
		}
		return false;
	}

	/* 
	 * 根据orderID 查询 对应的订单详情
	 */
	@Override
	public List<OrderDetail> findOrderDetailByOrderID(String orderID) {
		Connection conn = null;
		ArrayList<OrderDetail> list = new ArrayList<OrderDetail>();
		
		try {
			conn = DBHelper.getConnection();
			
			ResultSet set = odd.selectOrderDetailByOrderID(conn, orderID);
			while (set.next()) {
				int id = set.getInt("id");
				int quantity = set.getInt("quantity");
				double discount = set.getDouble("discount");
				String orderid = set.getString("orderid");
				String orderDate = set.getString("order_date");
				int customerID = set.getInt("customerid");
				String customerName = set.getString("customer_name");
				
				int empID = set.getInt("empid");
				String empName = set.getString("emp_name");
				
				int productId = set.getInt("productid");
				String productName = set.getString("product_name");
			
				
				list.add(new OrderDetail(id,quantity, discount, 
						new Orders(orderID, orderDate, new Customers(customerID, customerName), new Employees(empID, empName)), 
						new Products(productId, productName)));
			}
			return list;
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			odd.close();
		}
		
		return null;
	}

	/* 
	 * 修改订单-详情
	 */
	@Override
	public boolean modifyOrderDetails(Orders order, OrderDetail... orderDetail) {
		
		Connection conn = null;
		
		try {
			conn = DBHelper.getConnection();
			
			// 开启事务
			conn.setAutoCommit(false);
			odd.updateOrderDetails(conn, orderDetail);
			odi.updateOrder(conn, order);
			conn.commit();
			conn.setAutoCommit(true);
			
			return true;
		} catch (SQLException e) {
			
			try {
				conn.rollback();
				conn.setAutoCommit(true);
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			
			e.printStackTrace();
		} finally {
			odi.close();
			odd.close();
		}
		
		return false;
	}

	/* 
	 * 根据orderID 查询 对应的订单详情的数量
	 */
	@Override
	public int findDetailCountByOrderID(String orderID) {
		Connection conn = null;
		int count = 0;
		try {
			conn = DBHelper.getConnection();
			ResultSet set = odd.selectDetailCountByOrderID(conn, orderID);
			while (set.next()) {
				count = set.getInt(1);
			}
			return count;
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			odd.close();
		}
		return 0;
	}

	/* 
	 * 新建订单-详情 根据订单id
	 */
	@Override
	public boolean addDetailsByOrderId(OrderDetail... orderDetail) {
		Connection conn = null;
		
		try {
			conn = DBHelper.getConnection();
			// 开启事务处理
			conn.setAutoCommit(false);
			odd.insertDetailByOrderId(conn , orderDetail);
			conn.commit();
			conn.setAutoCommit(true);
			
			return true;
		} catch (SQLException e) {
			try {
				conn.rollback();
				conn.setAutoCommit(true);
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			
			e.printStackTrace();
		} finally {
			odd.close();
		}
			
		return false;
	}

	/* 
	 * 根据id 查询 对应的订单详情
	 */
	@Override
	public int findDetailByIds(String orderId  , int detailId , int productId) {
		Connection conn = null;
		int count = 0;
		try {
			conn = DBHelper.getConnection();
			ResultSet set = odd.selectDetailByIds(conn, orderId, detailId , productId);
			while (set.next()) {
				count = set.getInt(1);
			}
			return count;
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			odd.close();
		}
		
		return count;
	}

	/* 
	 * 根据detailid 删除订单-详情
	 */
	@Override
	public int removeDetailById(int detailId) {
		Connection conn = null;
		
		try {
			conn = DBHelper.getConnection();
			
			conn.setAutoCommit(false);
			int i = odd.deleteDetailById(conn, detailId);
			conn.commit();
			conn.setAutoCommit(true);
			
			return i;
		} catch (SQLException e) {
			
			try {
				conn.rollback();
				conn.setAutoCommit(true);
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
			e.printStackTrace();
		} finally {
			odd.close();
		}
		
		return 0;
	}

	/* 
	 * 根据orderid 删除订单-详情
	 */
	@Override
	public boolean removeDetailByOrderId(String orderId) {
		Connection conn = null;
		
		try {
			conn = DBHelper.getConnection();
			
			conn.setAutoCommit(false);
			odd.deleteDetailByOrder(conn, orderId);
			odi.deleteOrderByOrderId(conn, orderId);
			conn.commit();
			conn.setAutoCommit(true);
			
			return true;
		} catch (SQLException e) {
			try {
				conn.rollback();
				conn.setAutoCommit(true);
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
			e.printStackTrace();
		} finally {
			odd.close();
			odi.close();
		}
		
		return false;
	}

}
